package com.example.module.ai.websocket;

import com.example.framework.websocket.core.session.NativeWebSocketHandler;
import com.example.module.ai.api.AiApi;
import com.example.module.ai.api.SparkAiWebSocketAiApiImpl;
import jakarta.websocket.OnMessage;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@ServerEndpoint("/websocket/spark-ai")
@Slf4j
public class SparkAiServer extends NativeWebSocketHandler {

    @Override
    @OnMessage // 必须要加此注解
    public void onMessage(String message, Session session) {
        // 运行时的 WebSocket 连接对象，也就是端点实例，是由服务器创建，而不是 Spring，所以不能使用自动装配
        AiApi aiService = applicationContext.getBean(SparkAiWebSocketAiApiImpl.class);
        String answer = aiService.completion(message);
        session.getAsyncRemote().sendText(answer);
    }

}
